<template>
    <div class="app-container">
        <div>
            <p class="dialogTitle title">当前地震事件:
                <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
                <span v-else>请在地震事件管理中设置当前地震事件</span>
            </p>
        </div>

        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
            <el-form-item label="生命线类型" prop="lineType">
                <el-select v-model="queryParams.lineType" @change="chooseLineType" placeholder="请选择生命线类型" clearable>
                  <el-option v-for="item in option" :key="item.dictItemCode" :label="item.dictItemName"
                      :value="item.dictItemCode">
                  </el-option>
                </el-select>
                &nbsp;—&nbsp;
								<el-select v-model="queryParams.lineType2" placeholder="请选择" clearable>
									<el-option v-for="item in lineTypeArr2" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
									</el-option>
								</el-select>
            </el-form-item>
            <el-form-item label="破坏情况" prop="damage">
                <el-select v-model="queryParams.damage" placeholder="请选择破坏情况" clearable>
                    <el-option v-for="item in option1" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="推测烈度" prop="intensity">
                <el-select v-model="queryParams.intensity" placeholder="请选择推测烈度" clearable>
                    <el-option v-for="item in option2" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="生命线工程编号" prop="lifelineCode">
                <el-input v-model="queryParams.lifelineCode" placeholder="请输入生命线工程编号" clearable />
            </el-form-item>
            <el-form-item label="生命线工程名称" prop="lifelineName">
                <el-input v-model="queryParams.lifelineName" placeholder="请输入生命线工程名称" clearable />
            </el-form-item>
            <el-form-item label="调查员" prop="investigator">
                <el-input v-model="queryParams.investigator" placeholder="请输入调查员" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
        	<el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent" @click="handleImport">导入</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent||dataList.length==0" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe>
            <el-table-column label="序号" align="center" width="55">
                <template #default="scope">
                    <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="生命线工程编号" align="center" prop="lifelineCode" />
                
            <el-table-column label="抽样点名称" align="center" prop="sampleName" />
            <el-table-column label="生命线工程名称" align="center" prop="lifelineName" >
            	<template #default="scope">
                    <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{
                            scope.row.lifelineName
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column label="推测烈度" align="center" prop="intensity">
            	<template #default="scope">
                    {{getIntensity(scope.row.intensity)}}
                </template>
            </el-table-column>
            <el-table-column label="生命线类型" align="center" prop="lineType" >
            	<el-table-column label="一级" align="center" prop="lineType" width="140">
								<template #default="scope">
				          {{getLineType(scope.row.lineType)}}
				        </template>
				      </el-table-column>
							<el-table-column label="二级" align="center" prop="lineType2">
								<template #default="scope">
				          {{getLineType2(scope.row.lineType2)}}
				        </template>
				     </el-table-column>
            </el-table-column>
            <el-table-column label="破坏情况" align="center" prop="damage">
                <template #default="scope">
                    {{getDamage(scope.row.damage)}}
                </template>
            </el-table-column>
            <el-table-column label="调查员" align="center" prop="investigator" />
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />


        <el-dialog title="查看生命工程数据" v-model="open" width="900px" append-to-body>
            <el-form ref="addRef" label-width="120px" :model="form">
                <el-row style="margin: 20px 0;">
                    <el-col :span="24">
                        <el-form-item label="行政区划" required>
                            <span>{{ form.province }}{{ form.city }}{{ form.district }}{{ form.detailAddress }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="抽样点名称" prop="sampleName" required>
                            <el-input v-model="form.sampleName" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="经度" prop="longitude" required>
                            <el-input v-model="form.longitude" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="纬度" prop="latitude" required>
                            <el-input v-model="form.latitude" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="生命线工程名称" prop="lifelineName" required>
                            <el-input v-model="form.lifelineName" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="生命线类型" prop="lineType" required>
                            <el-select v-model="form.lineType" disabled>
                                <el-option v-for="item in option" :key="item.dictItemCode" :label="item.dictItemName"
                                    :value="item.dictItemCode">
                                </el-option>
                            </el-select>
                            &nbsp;—&nbsp;
														<el-select v-model="form.lineType2" disabled>
															<el-option v-for="item in lineTypeArr2" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
															</el-option>
														</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="破坏情况">
                            <el-select v-model="form.damage" disabled>
                                <el-option v-for="item in option1" :key="item.dictItemCode" :label="item.dictItemName"
                                    :value="item.dictItemCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="推测烈度" prop="intensity" required>
                            <el-select v-model="form.intensity" disabled>
                                <el-option v-for="item in option2" :key="item.dictItemCode" :label="item.dictItemName"
                                    :value="item.dictItemCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="具体描述情况">
                            <el-input v-model="form.detailDesc" type="textarea" maxlength="500" show-word-limit
                                :autosize="{ minRows: 5, maxRows: 5 }" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="照片">
                            <file-upload v-model="form.enclosurePhoto" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="视频">
                            <file-upload v-model="form.enclosureVideo" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="音频">
                            <file-upload v-model="form.enclosureAudio" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="相册">
                            <file-upload v-model="form.enclosureAlbum" disabled />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="open = false">关 闭</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 选择抽样点对话框 -->
        <el-dialog title="选择抽样点" v-model="selectPoint" width="1200px" append-to-body>
        	<p></p>
        	<el-table v-loading="loading1" :data="dataList1" stripe row-key="id" @row-click="singleElection">
            <el-table-column align="center" width="80" label="请选择">
				      <template #default="scope">
				      <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
				      <el-radio
				      class="radio"
				      v-model="templateSelection"
				      :label="scope.row.samplingPointCode"
				      ><span></span></el-radio>
				      </template>
      			</el-table-column>
            <!-- <el-table-column label="评估子区名称" align="center" prop="evaluationSubareaName" />
            <el-table-column label="评估子区类别" align="center" prop="evaluationSubareaType" /> -->
            <el-table-column label="抽样点编号" align="center" prop="samplingPointCode" />
            <el-table-column label="抽样点名称" align="center" prop="samplingPointName" />
            <!-- <el-table-column label="省" align="center" prop="province" />
            <el-table-column label="市" align="center" prop="city" />
            <el-table-column label="县" align="center" prop="area" />
            <el-table-column label="详细地址" align="center" prop="address" /> -->
            <el-table-column label="创建人" align="center" prop="createUserName" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="100">
	            <template #default="scope">
	               <span>{{ parseTime(scope.row.createTime) }}</span>
	            </template>
	         	</el-table-column>
        	</el-table>
        	<pagination v-show="total1 > 0" :total="total1" v-model:page="queryParams1.page" v-model:limit="queryParams1.rows"
            @pagination="getList1" />
        	<template #footer>
              <div class="dialog-footer">
                  <el-button type="primary" @click="nextStep">下一步</el-button>
                  <el-button @click="selectPoint = false">取 消</el-button>
              </div>
          </template>
        </el-dialog>
        <!-- 导入对话框 -->
        <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
          <p></p>
          <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
              :disabled="upload.isUploading" :before-upload="handleFileBeforeUpload"
              :on-progress="handleFileUploadProgress" :on-error="handleFileUploadError" :on-success="handleFileSuccess"
              :data="templateData" :auto-upload="false" drag>
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <template #tip>
                  <div class="el-upload__tip text-center">
                      <span>仅允许导入xls、xlsx格式文件。</span>
                       <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                          @click="importTemplate('生命线工程数据.xlsx')">下载模板</el-link> 
                  </div>
              </template>
          </el-upload>
          <template #footer>
              <div class="dialog-footer">
                  <el-button type="primary" @click="submitFileForm">确 定</el-button>
                  <el-button @click="upload.open = false">取 消</el-button>
              </div>
          </template>
        </el-dialog>
    </div>
</template>

<script setup name="lifelineEngineerCheck">
import useEventStore from "@/store/modules/event";
import { getAllList, getDetail, getLife, getSamPoiByXsemId } from "@/api/disasterManage/fieldSurveyManage/lifelineEngineerCheck.js"
import { getToken } from "@/utils/auth";
import { getDict } from "@/api/system/dict";
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const dataList1 = ref([]);
const ids = ref();
const loading = ref(false);
const loading1 = ref(false);
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const selectPoint = ref(false);
const total = ref(0);
const total1 = ref(0);
const templateSelection = ref("");
const templateData = ref({});
const showSearch = ref(true);
const option = ref([]);
const lineTypeArr2 = ref([]);
const option1 = ref([]);
const option2 = ref([]);
const lineTypeArr20 = ref([]);
const lineTypeArr21 = ref([]);
const lineTypeArr22 = ref([]);
const lineTypeArr23 = ref([]);
const lineTypeArr24 = ref([]);
const lineTypeArr25 = ref([]);
const lineTypeArr26 = ref([]);
const lineTypeArr27 = ref([]);
const lineTypeArr2All = ref([]);
const currentEvent = computed(() => useEventStore().currentEvent);
/*** 导入参数 */
const upload = reactive({
    open: false,
    title: "",
    isUploading: false,
    headers: { token: "token " + getToken() },
    url: import.meta.env.VITE_APP_BASE_API + "/lifeline/importExcel"
});
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        lineType: undefined,
        lineType2: undefined,
        damage: undefined,
        intensity: undefined,
        lifelineCode: undefined,
        lifelineName: undefined,
        investigator: undefined,
    },
    queryParams1: {
        page: 1,
        rows: 10,
    },
    form: {}
})
const { queryParams, queryParams1, form } = toRefs(data)

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    queryParams.value.lineType2 = "";
    lineTypeArr2.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
    const query={
        ...queryParams.value, xsemId: currentEvent.value.id 
    }
    proxy.downloadTemplate('/lifeline/exportData', {...query}, `生命工程数据${new Date().getTime()}.xls`)
}
// 导出模版
function importTemplate(name) {
    proxy.downloadTemplate("/template/download", {
        fileName: name
    }, name);
};

//单选
function singleElection(row) {
  templateSelection.value = row.samplingPointCode;
  templateData.value = row;
  //console.log(templateSelection.value)

}
/** 打开选择抽样点弹窗 */
function handleImport() {
	selectPoint.value = true;    
    templateSelection.value = '';
  getList1();
    
}
//获取抽样点数据列表
function getList1(){
	if (currentEvent.value) {
      loading1.value = true;
      getSamPoiByXsemId({ ...queryParams1.value, xsemId: currentEvent.value.id }).then(response => {
          dataList1.value = response.data.rows ? response.data.rows : [];
          total1.value = response.data.total;
          loading1.value = false;
      });
  } else {
      dataList1.value = [];
      total1.value = 0;
  }
}
/** 打开导入表弹窗 */
function nextStep() {
	if(!templateSelection.value){
		proxy.$modal.msgWarning("请选择抽样点");
		return;
	}
  upload.title = "导入";
  upload.open = true;
}
/** 提交上传文件 */
function submitFileForm() {
    proxy.$refs["uploadRef"].submit();
};



/**文件上传前处理 */
function handleFileBeforeUpload(file) {
    proxy.$modal.loading("正在上传文件，请稍候...");
    return true
}

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
    upload.isUploading = true;
};
// 上传失败
function handleFileUploadError(err) {
    proxy.$modal.closeLoading();
    proxy.$modal.msgError("上传文件失败");
}
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
    upload.open = false;
    upload.isUploading = false;
    proxy.$modal.closeLoading();
    proxy.$refs["uploadRef"].handleRemove(file);
    proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +  response.data?response.data:response.message + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
    getList();
};
// 编辑
function handleEdit(row) {
    detail(row.xlrId);
}

// 查看
function detail(id) {
    getDetail({ id: id }).then(res => {
        open.value = true;
        proxy.resetForm("addRef");
        form.value = res.data;
        editLineType(form.value.lineType);
    })
}

/** 查询数据列表 */
function getList() {
    if (currentEvent.value) {
        loading.value = true;
        getAllList({ ...queryParams.value, xsemId: currentEvent.value.id }).then(response => {
            dataList.value = response.data.rows ? response.data.rows : [];
            total.value = response.data.total;
            loading.value = false;
        });
    } else {
        dataList.value = [];
        total.value = 0;
    }
}
/** 获取字典值 */
async function getDictData() {
	await getDict("smxphqk").then(response => {
	    option1.value = response.data;
	});
	await getDict("tcld").then(response => {
	    option2.value = response.data;
	});
	await getDict("smxlx").then(response => {
	    option.value = response.data;
	});
}
async function getDictData2(){
	
	await getDict("jtxt").then(response => {
    lineTypeArr21.value = response.data;
	});

	await getDict("gsxt").then(response => {
    lineTypeArr22.value = response.data;
	});
	await getDict("syxt").then(response => {
    lineTypeArr23.value = response.data;
	});
	await getDict("rqxt").then(response => {
    lineTypeArr24.value = response.data;
	});
	await getDict("dlxt").then(response => {
    lineTypeArr25.value = response.data;
	});
	await getDict("txxt").then(response => {
    lineTypeArr26.value = response.data;
	});
	await getDict("slgc").then(response => {
    lineTypeArr27.value = response.data;
	});
	lineTypeArr2All.value = [...lineTypeArr21.value,...lineTypeArr22.value
	,...lineTypeArr23.value,...lineTypeArr24.value,...lineTypeArr25.value,...lineTypeArr26.value,...lineTypeArr27.value]
//	console.log(structureTypeArr2All.value)
}
function editLineType(value){
	if(value=="1"){
		getDict("jtxt").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else if(value=="2"){
		getDict("gsxt").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else if(value=="3"){
		getDict("syxt").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else if(value=="4"){
		getDict("rqxt").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else if(value=="5"){
		getDict("dlxt").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else if(value=="6"){
		getDict("txxt").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else if(value=="7"){
		getDict("slgc").then(response => {
	    lineTypeArr2.value = response.data;
		});
	}else{
		lineTypeArr2.value = [];
	}
}
function getDamage(val){
  var name = ''
  option1.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function getIntensity(val){
  var name = ''
  option2.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function getLineType(val){
  var name = ''
  option.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function getLineType2(val){
  var name = ''
  lineTypeArr2All.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function chooseLineType(value) {
		queryParams.value.lineType2 = "";
		if(value=="1"){
			getDict("jtxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="2"){
			getDict("gsxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="3"){
			getDict("syxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="4"){
			getDict("rqxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="5"){
			getDict("dlxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="6"){
			getDict("txxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="7"){
			getDict("slgc").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else {			
		  lineTypeArr2.value = [];
			
		}
		
	}
// 获取生命线类型
//function getLifes(){
//  getLife().then(res=>{
//      console.log(res.data,'2222');
//      option.value = res.data
//  })
//}

onMounted(() => {
    setTimeout(() => {
        getList();
    }, 200)
})
getDictData();
getDictData2();
//getLifes();
</script>

<style scoped lang="scss">
.title {
    margin: 0 0 20px !important;
}
</style>

